package com.evil.common.application.enums.permission;

import com.evil.common.core.enums.BaseEnum;
import com.evil.common.family.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * 应用权限类型
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum ApplicationPermissionTypeEnum {

    /**
     * 应用权限类型
     */
    USER(1, "用户", ApplicationPermissionSignEnum.SIGN_USER),
    DEPARTMENT(2, "部门", ApplicationPermissionSignEnum.SIGN_DEPARTMENT),
    ROLE(3, "角色", ApplicationPermissionSignEnum.SIGN_ROLE),
    DYNAMIC_APPLICANT(4, "动态申请人", ApplicationPermissionSignEnum.SIGN_DYNAMIC),
    DYNAMIC_DEPARTMENT_MANAGER(5, "动态部门主管", ApplicationPermissionSignEnum.SIGN_DYNAMIC),
    DYNAMIC_MEMBER_CONTROL(6, "动态成员控件", ApplicationPermissionSignEnum.SIGN_DYNAMIC),
    DYNAMIC_DEPARTMENT_CONTROL(7, "动态部门控件", ApplicationPermissionSignEnum.SIGN_DYNAMIC),
    DYNAMIC_DEPARTMENT(8, "动态部门", ApplicationPermissionSignEnum.SIGN_DYNAMIC),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;
    @Getter
    private final ApplicationPermissionSignEnum signEnum;

    public boolean isDynamic() {
        return this.signEnum.isDynamic();
    }

    public static boolean isDynamic(Integer id) {
        return ApplicationPermissionTypeEnum.isExistById(id).isDynamic();
    }

    public static Optional<ApplicationPermissionTypeEnum> findById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static ApplicationPermissionTypeEnum isExistById(Integer id) {
        return ApplicationPermissionTypeEnum.findById(id).orElseThrow(ApplicationPermissionTypeEnum::getBusinessException);
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(BaseEnum.ERROR_PARAMETER, "权限类型参数错误");
    }
}
